﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HZ.Supervise.Model;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model.Query;
using System.Text;
using System.Data;

namespace HZ.Supervise.Web.Common
{
    public partial class SelectGoods : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                //绑定药品
                Bind();

            }
        }

        private void Bind()
        {
            string _strSortId = Request.QueryString["SortId"].ToString();
            //string _SortSource = Request.QueryString["SortSource"].ToString();
            string _SortSource = "1";
            int Total=0;
            BLL.BLL_Goods bllGoods = new HZ.Supervise.BLL.BLL_Goods();
            DataTable dt = bllGoods.GetGoodsBySortID(txtProductName.Text,_strSortId, _SortSource,AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out Total);
            AspNetPager1.RecordCount = Total;

            gvwGoods.DataSource = dt;
            gvwGoods.DataBind();
        }

        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkAll_OnClick(object sender, EventArgs e)
        {
            string[] param = this.hidAll.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            int cnt;

            foreach (GridViewRow item in this.gvwGoods.Rows)
            {
                ListItem li = new ListItem();
                cnt = 0;
                foreach (string str in param)
                {
                    if (((HiddenField)item.Cells[0].FindControl("HidID")).Value == str)
                    {
                        cnt++;
                    }
                }

                if (cnt == 0)
                {

                    this.hidAll.Value += ((HiddenField)item.Cells[0].FindControl("HidID")).Value + ",";//id
                    ((CheckBox)item.Cells[0].FindControl("chkCompany")).Checked = true;
                    this.hidAllName.Value += ((HiddenField)item.Cells[0].FindControl("HidName")).Value + ",";

                    li.Text = ((HiddenField)item.Cells[0].FindControl("HidName")).Value;
                    li.Value = ((HiddenField)item.Cells[0].FindControl("HidID")).Value;
                    lbCompany.Items.Add(li);
                }
            }

            ////this.DataBind();
            //gvwHospital.DataBind();
        }

        /// <summary>
        /// 全不选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UnchkAll_OnClick(object sender, EventArgs e)
        {
            string[] param = this.hidAll.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] paramName = this.hidAllName.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (GridViewRow item in this.gvwGoods.Rows)
            {
                ListItem li = new ListItem();
                li.Text = ((HiddenField)item.Cells[0].FindControl("HidName")).Value;
                li.Value = ((HiddenField)item.Cells[0].FindControl("HidID")).Value;
                lbCompany.Items.Remove(li);
                //id
                for (int i = 0; i < param.Length; i++)
                {
                    if (((HiddenField)item.Cells[0].FindControl("HidID")).Value == param[i])
                    {
                        param[i] = "";
                        ((CheckBox)item.Cells[0].FindControl("chkCompany")).Checked = false;
                    }

                }

                //name
                for (int i = 0; i < paramName.Length; i++)
                {
                    if (((HiddenField)item.Cells[0].FindControl("HidName")).Value == paramName[i])
                    {
                        paramName[i] = "";

                    }
                }

            }

            this.hidAll.Value = "";
            this.hidAllName.Value = "";
            for (int i = 0; i < param.Length; i++)
            {
                if (param[i] != "")
                {
                    this.hidAll.Value += param[i] + ",";
                }
            }
            for (int i = 0; i < paramName.Length; i++)
            {
                if (paramName[i] != "")
                {
                    this.hidAllName.Value += paramName[i] + ",";
                }
            }
            //this.DataBind();
        }
        /// <summary>
        /// 单选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkCompany_OnCheckedChanged(object sender, EventArgs e)
        {
            CheckBox ckb = sender as CheckBox;

            if (ckb.Checked == true)
            {
                this.hidAll.Value += ((HiddenField)ckb.Parent.FindControl("HidID")).Value + ",";
                this.hidAllName.Value += ((HiddenField)ckb.Parent.FindControl("HidName")).Value + ",";
                //添加listbox
                ListItem li = new ListItem();
                li.Value = ((HiddenField)ckb.Parent.FindControl("HidID")).Value;
                li.Text = ((HiddenField)ckb.Parent.FindControl("HidName")).Value;
                lbCompany.Items.Add(li);
            }
            else
            {
                //id
                string[] param = this.hidAll.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                this.hidAll.Value = "";

                foreach (string item in param)
                {
                    if (item != ((HiddenField)ckb.Parent.FindControl("HidID")).Value)
                    {
                        this.hidAll.Value += item + ",";
                    }
                }

                //name
                string[] paramName = this.hidAllName.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                this.hidAllName.Value = "";

                foreach (string item in paramName)
                {
                    if (item != ((HiddenField)ckb.Parent.FindControl("HidName")).Value)
                    {
                        this.hidAllName.Value += item + ",";
                    }
                }
                //移除listbox
                ListItem li = new ListItem();
                li.Value = ((HiddenField)ckb.Parent.FindControl("HidID")).Value;
                li.Text = ((HiddenField)ckb.Parent.FindControl("HidName")).Value;
                lbCompany.Items.Remove(li);
            }
        }
        /// <summary>
        /// 行绑定checkbox值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwGoods_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                string[] param = this.hidAll.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string item in param)
                {
                    if (item == ((HiddenField)e.Row.Cells[0].FindControl("HidID")).Value)
                    {
                        ((CheckBox)e.Row.Cells[0].FindControl("chkCompany")).Checked = true;
                        break;
                    }
                }
            }
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnselect_Click(object sender, EventArgs e)
        {
            Bind();
        }
      
    }
}
